﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;

namespace GPS.Controllers.GPS_TheoDoiBienDong
{
    public class GPS_BienDongGiaoVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        #region index

        public ActionResult Index(ViewModelDGV SearchModel, int? currentPage)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pGV_DVGTVT_MA);
            SearchModel.pGV_DVGTVT_MA = getdvgtvt_Ma;
            TempData["DVGTVT_MA"] = getdvgtvt_Ma;

            initialCategorySearchAction(SearchModel);
            string hodem = null;
            string ten = null;

            if (SearchModel.pGV_TEN != null)
            {
                string str = SearchModel.pGV_TEN.Trim();
                int idx = str.LastIndexOf(' ');
                if (idx != -1)
                {
                    hodem = str.Substring(0, idx);
                    ten = str.Substring(idx + 1);
                }
                else
                {
                    hodem = SearchModel.pGV_TEN;
                    ten = SearchModel.pGV_TEN;
                }
            }
            var entities = _db.Filter<DGV>(o => (SearchModel.pGV_MA == null || o.GV_MA.Contains(SearchModel.pGV_MA))
                                                && (SearchModel.pGV_TEN == null
                                                    || o.GV_HODEM.ToUpper().Trim().Contains(hodem.ToUpper())
                                                    || o.GV_TEN.ToUpper().Trim().Contains(ten.ToUpper())
                                                    || (o.GV_HODEM.ToUpper().Trim().Contains(hodem.ToUpper().Trim())
                                                    && o.GV_TEN.ToUpper().Trim().Contains(ten.ToUpper().Trim())))
                                                 && (SearchModel.pGV_DVGTVT_MA == null || o.GV_DVGTVT_MA == SearchModel.pGV_DVGTVT_MA)
                                                 && (SearchModel.pGV_HANGDT_MA == null || o.GV_HANGDT_MA == SearchModel.pGV_HANGDT_MA)
                                                 && (SearchModel.pGV_SOCMT == null || o.GV_SOCMT.Contains(SearchModel.pGV_SOCMT.Trim()))
                                                 && (SearchModel.pGV_TEL == null || o.GV_TEL.Contains(SearchModel.pGV_TEL.Trim()))
                                                 && (SearchModel.pGV_ACTIVE == null || o.GV_ACTIVE == SearchModel.pGV_ACTIVE)
                                                 );
            ViewBag.TotalRow = entities == null ? "0" : entities.Count().ToString();

            var model = new List<DGV>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DGV>(currentEntity));
            }

            int icount = 1;
            foreach (var item in model)
            {
                item.STT = icount.ToString();
                icount++;
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        public void initialCategorySearchAction(ViewModelDGV viewModel)
        {

            ViewBag.GV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        }
        #endregion

        #region Detail
        public ActionResult _Detail(string id1, string id2)
        {
            string _GV_MA = id1;
            string _GV_DVGTVT_MA = id2;
            CrudModelDGV _model = new CrudModelDGV();
            DGV model = _db.GetOne<DGV>(x => x.GV_MA == _GV_MA && x.GV_DVGTVT_MA == _GV_DVGTVT_MA);
            _model = CopyObject_model(model);
            initialCategoryDetailAction(_model);
            return PartialView("_Detail", _model);
        }
        public CrudModelDGV CopyObject_model(DGV model)
        {
            CrudModelDGV _model = new CrudModelDGV
            {
                GV_MA = model.GV_MA,
                GV_DVGTVT_MA = model.GV_DVGTVT_MA,
                GV_HANGDT_MA = model.GV_HANGDT_MA,
                GV_HODEM = model.GV_HODEM,
                GV_TEN = model.GV_TEN,
                GV_NGAYSINH = model.GV_NGAYSINH,
                GV_SOCMT = model.GV_SOCMT,
                GV_NOICT = model.GV_NOICT,
                GV_NOICT_DVHC_MA = model.GV_NOICT_DVHC_MA,
                GV_NOICT_DVGTVT_MA = model.GV_NOICT_DVGTVT_MA,
                GV_GIOITINH = model.GV_GIOITINH,
                GV_TEL = model.GV_TEL,
                GV_HTTUYENDUNG = model.GV_HTTUYENDUNG,
                GV_TDVANHOA = model.GV_TDVANHOA,
                GV_TDCHUYENMON = model.GV_TDCHUYENMON,
                GV_TDSUPHAM = model.GV_TDSUPHAM,
                GV_HGPLX_MA = model.GV_HGPLX_MA,
                GV_NGAYCAPGPLX = model.GV_NGAYCAPGPLX,
                GV_THAMNIENLX = model.GV_THAMNIENLX,
                GV_SOQD_GCN = model.GV_SOQD_GCN,
                GV_NGAYQD_GCN = model.GV_NGAYQD_GCN,
                GV_HTDTAO_MA = model.GV_HTDTAO_MA,
                GV_CAUTAOSUACHUA = (model.GV_CAUTAOSUACHUA == "0" ? false : true),
                GV_DAODUCLAIXE = (model.GV_DAODUCLAIXE == "0" ? false : true),
                GV_NGHIEPVUVANTAI = (model.GV_NGHIEPVUVANTAI == "0" ? false : true),
                GV_LUATGTDB = (model.GV_LUATGTDB == "0" ? false : true),
                GV_KYTHUATLAIXE = (model.GV_KYTHUATLAIXE == "0" ? false : true),
                GV_MOTA = model.GV_MOTA,
                GV_ACTIVE = (model.GV_ACTIVE == "0" ? false : true),
                GV_NGUOINHAP = "SYSTEM",
                GV_ANHCD = model.GV_ANHCD
            };
            return _model;
        }
        public void initialCategoryDetailAction(CrudModelDGV view)
        {
            if (view.GV_ACTIVE == true)
            {
                ViewBag.txtTrangThai = "Hiệu Lực";
            }
            else
            {
                ViewBag.txtTrangThai = "Không Hiệu Lực";
            }

            ViewBag.GV_DVGTVT_MAs = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"
                                                            && o.DVGTVT_MA == view.GV_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.GV_HANGDT_MAs = _db.Filter<SHANGDT>(o => o.HANGDT_MA == view.GV_HANGDT_MA).Select(x => x.HANGDT_TEN).FirstOrDefault();
            ViewBag.GV_HGPLX_MAs = _db.Filter<SHANGGPLX>(o => o.HGPLX_MA == view.GV_HGPLX_MA).Select(x => x.HGPLX_TEN).FirstOrDefault();
            ViewBag.GV_NOICT_DVGTVT_MAs = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"
                                                                && o.DVGTVT_MA == view.GV_NOICT_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.GV_NOICT_DVHC_MAs = _db.Filter<SDONVIHC>(o => o.DVHC_MA == view.GV_NOICT_DVHC_MA).Select(x => x.DVHC_TEN).FirstOrDefault();
            ViewBag.GV_HTDTAO_MAs = _db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1").Select(x => x.HTDT_MOTA).FirstOrDefault();
            ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        }
        #endregion

        #region"Biến động giáo viên"
        [HttpGet]
        public ActionResult _Edit(string id1, string id2)
        {
            string _GV_MA = id1;
            string _GV_DVGTVT_MA = id2;
            DGV model = _db.GetOne<DGV>(x => x.GV_MA == _GV_MA && x.GV_DVGTVT_MA == _GV_DVGTVT_MA);
            CrudModelDLSGV _model = new CrudModelDLSGV();
            _model.LSGV_GV_MA_CU = model.GV_MA;
            _model.LSGV_DVGTVT_MA_CU = model.GV_DVGTVT_MA;
            ViewBag.DVGTVT_TEN = _db.GetOne<SDONVIGTVT>(x => x.DVGTVT_MA == model.GV_DVGTVT_MA && x.DVGTVT_MAQL == "40").DVGTVT_TEN;
            _model.LSGV_NGAYQD_GCN_CU = model.GV_NGAYQD_GCN;
            ViewBag.GV_TEN = model.GV_HODEM + " " + model.GV_TEN;
            ViewBag.GV_HGPLX_MA = model.GV_HGPLX_MA;
            ViewBag.DVGTVT_MA_MOIs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA != _GV_DVGTVT_MA), "DVGTVT_MA", "DVGTVT_TEN");
            return PartialView("_Edit", _model);
        }

        [HttpPost]
        public ActionResult _Edit(CrudModelDLSGV model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                DLSGV _model = new DLSGV();
                GPS.COMMON.Helpers.CopyObject<DLSGV>(model, ref _model);
                _model.LSGV_NGAYNHAP = DateTime.Now;
                _model.LSGV_NGUOINHAP = "admin";
                if (_model.LSGV_STATUS != "4")
                    _model.LSGV_DVGTVT_MA_MOI = null;
                _model.ID = EFSequence.GetNextValue("SEQ_DLSGV_ID");
                _db.Insert<DLSGV>(_model);
                //update trạng thái DGV:
                string _id1 = model.LSGV_GV_MA_CU;
                string _id2 = model.LSGV_DVGTVT_MA_CU;
                var _dgv = _db.GetOne<DGV>(o => o.GV_MA == _id1 && o.GV_DVGTVT_MA == _id2);
                _dgv.GV_ACTIVE = model.LSGV_STATUS;
                _db.Update<DGV>(_dgv);
                string url = Url.Action("Index", "GPS_BienDongGiaoVien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }
        #endregion
    }
}